package com.diors.commons.config.properties;

import java.lang.reflect.Field;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.diors.commons.utils.PropertiesUtils;



/**
 * 写入Properties
 * @author 杜祥
 *
 */
public class ConfigurableWrite
{
	
	private static Logger log = Logger.getLogger(ConfigurableWrite.class);
	
	@SuppressWarnings("rawtypes")
	public static void write(String filename,Object obj) throws Exception
	{
		if(obj == null) return;
		Field[] fs;
		Class clazz;
		if(obj instanceof Class)
		{
			clazz = (Class) obj;
			fs = clazz.getFields();
		}
		else
		{
			fs = obj.getClass().getFields();
		}
		Properties p = new Properties();
		Property property;
		for(Field f : fs)
		{
			property = f.getAnnotation(Property.class);
			if(property == null)
			{
				log.warn("元素: " + f.getName() + " 类： " + f.getDeclaringClass().getName() + "没有Property类型注释");
			}
			if(property.key().isEmpty())
			{
				log.warn("元素: " + f.getName() + " 类： " + f.getDeclaringClass().getName() + "注释为leng为0");
			}
			p.setProperty(property.key(), f.get(f.getType())+"");
		}
		PropertiesUtils.write(filename, p);
	}
}
